package com.bilibili.juc.cf.one;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class FutureWhenCompleteTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<String> orgFuture = CompletableFuture.supplyAsync(
                ()->{
                    System.out.println("supplyAsync 当前线程名称：" + Thread.currentThread().getName());
                    try {
                        Thread.sleep(2000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return "捡田螺的小男孩";
                }
        );

        CompletableFuture<String> rstFuture = orgFuture.whenComplete((a, throwable) -> {
            System.out.println("whenComplete 当前线程名称：" + Thread.currentThread().getName());
            System.out.println("上个任务执行完啦，还把结果为: \"" + a + "\" 传过来");
            if ("捡田螺的小男孩".equals(a)) {
                System.out.println("666");
                return;
            }
            System.out.println("233333");
        });

        System.out.println(rstFuture.get());
    }
}
//输出
/*
supplyAsync 当前线程名称：ForkJoinPool.commonPool-worker-1
whenComplete 当前线程名称：ForkJoinPool.commonPool-worker-1
上个任务执行完啦，还把结果为: "捡田螺的小男孩" 传过来
666
捡田螺的小男孩
* */